package com.ykdl.member.kid.gears;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.sitech.oncon.barcode.core.CaptureActivity;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.GetUser_ScoreBean;
import com.ykdl.member.kid.marketcard.CardIntroWebView;
import com.ykdl.member.kid.marketcard.CardPersonalInformationActivity;
import com.ykdl.member.kid.marketcard.MarketcardHelpAandIntro;
import com.ykdl.member.kid.marketcard.MyCardActivity;
import com.ykdl.member.views.IaskMenuBarView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class GoodLifeActivity extends BaseScreen implements OnClickListener{
	private IaskMenuBarView iask_menu_bar ;
	private ImageView imghead ,sy_level;
	private TextView yfl_score,sy_score,level,nickname;
	private BitmapUtils mBmpUtils;
	private LinearLayout sorc_bg ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("会员中心", 0, 0, null, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.goodlifeactivity_xml);
		inint();
		getData();
	}
	public void inint() {
		sorc_bg  = (LinearLayout)findViewById(R.id.sorc_bg);
		mBmpUtils = new BitmapUtils(this);
		mBmpUtils.configDefaultLoadingImage(R.drawable.head_default_person_center);
		mBmpUtils.configDefaultLoadFailedImage(R.drawable.head_default_person_center);
		findViewById(R.id.yaojifen).setOnClickListener(this);
		findViewById(R.id.mycard).setOnClickListener(this);
		findViewById(R.id.help).setOnClickListener(this);
		findViewById(R.id.edite).setOnClickListener(this);
		imghead = (ImageView)findViewById(R.id.imghead);
		sy_level  = (ImageView)findViewById(R.id.sy_level);
		yfl_score = (TextView)findViewById(R.id.yfl_score);
		sy_score = (TextView)findViewById(R.id.sy_score);
		sy_score.setOnClickListener(this);
		sy_level.setOnClickListener(this);
		level = (TextView)findViewById(R.id.level);
		nickname = (TextView)findViewById(R.id.nickname);
		iask_menu_bar = (IaskMenuBarView)findViewById(R.id.iask_menu_bar);
		iask_menu_bar.setSelectedID(R.id.bar_askdoctor);
		iask_menu_bar.setBaseScreen(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
	}
	public void getData() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.GET_CARD_SCORE;
		HttpParameters parameterss = new HttpParameters();
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss,null);
		 TaskManager.startHttpRequest(request, new getDataTag(),  GetUser_ScoreBean.class);
	}
	
	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof GetUser_ScoreBean) {
				GetUser_ScoreBean bean = (GetUser_ScoreBean)t;
				if (null==bean.getError()) {
					setData(bean);
				}else {
					Toast.makeText(mContext, "加载失败,"+bean.getDesc(), 1).show();
				}
				
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void setData(GetUser_ScoreBean bean) {
		nickname.setText(bean.getActor_info().getDisplay_name()+"");
//		yfl_score.setText(bean.getUser_score().getYfl_score()+"");
		if (bean.getUser_score().getSy_score()==0) {
			sorc_bg.setVisibility(View.GONE);
		}else {
			sorc_bg.setVisibility(View.VISIBLE);
		}
		sy_score.setText(bean.getUser_score().getSy_score()+"");
		level.setText("V"+bean.getUser_score().getLevel());
		if (bean.getUser_score().getLevel()==1) {
			sy_level.setBackgroundResource(R.drawable.v1_bg);
		}else if(bean.getUser_score().getLevel()==2){
			sy_level.setBackgroundResource(R.drawable.v2_bg);
		}else if(bean.getUser_score().getLevel()==3){
			sy_level.setBackgroundResource(R.drawable.v3_bg);
		}
		if (null!=bean.getActor_info().getAvatar()) {
			mBmpUtils.display(imghead, bean.getActor_info().getAvatar().getDownload_urls().getSmal().getUrl());
		}
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.yaojifen:
			go(CaptureActivity.class);
			break;
		case R.id.mycard:
			go(MyCardActivity.class);
			break;
		case R.id.help:
			Intent intent = new Intent(mContext,MarketcardHelpAandIntro.class);
			intent.putExtra(KidAction.IS_GONE_BT, false);
			GoodLifeActivity.this.startActivity(intent);
			break;
		case R.id.edite:
			go(CardPersonalInformationActivity.class);
			break;
		case R.id.sy_score:
			go(CardIntroWebView.class);
			break;
		case R.id.sy_level:
			go(CardIntroWebView.class);
			break;
		default:
			break;
		}
	}
}
